package com.ddmmo.exhibition.common.entity;

import com.ddmmo.exhibition.common.enums.ResponseCodeEnum;
import com.ddmmo.exhibition.common.exception.EmergencyException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一接口返回对象
 *
 * @author Wang Jiajun (20022567)
 * @date 2022/02/17
 */

@ApiModel(value="统一接口返回对象", description="统一接口返回对象")
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("成功标志")
    private Boolean success;

    @ApiModelProperty("返回代码")
    private Integer code;

    @ApiModelProperty("返回消息")
    private String msg;

    @ApiModelProperty("返回数据对象")
    private T data;

    @ApiModelProperty("返回时间戳")
    private long timestamp = System.currentTimeMillis();

    public static Result<?> success() {
        Result<?> result = new Result<>();
        result.setSuccess(Boolean.TRUE);
        result.setCode(ResponseCodeEnum.SUCCESS.getCode());
        return result;
    }

    public static <T>Result<T> success(String msg,T data) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.TRUE);
        result.setMsg(msg);
        result.setCode(ResponseCodeEnum.SUCCESS.getCode());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.TRUE);
        result.setCode(ResponseCodeEnum.SUCCESS.getCode());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.FALSE);
        result.setCode(ResponseCodeEnum.ERROR.getCode());
        result.setMsg(message);
        result.setData(null);
        return result;
    }

    public static Result<?> error(ResponseCodeEnum responseCodeEnum) {
        Result<?> result = new Result<>();
        if (responseCodeEnum != null) {
            result.setCode(responseCodeEnum.getCode());
            result.setMsg(responseCodeEnum.getMessage());
        }
        result.setSuccess(Boolean.FALSE);
        return result;
    }

    public static Result<?> error(EmergencyException exception) {
        Result<Object> result = new Result<>();
        if (exception != null) {
            result.setCode(exception.getCode());
            result.setMsg(exception.getMessage());
            result.setData(exception.getData());
        }
        result.setSuccess(Boolean.FALSE);
        return result;
    }

    public static Result<?> error(EmergencyException exception, String errorMsg) {
        Result<?> result = new Result<>();
        if (exception != null) {
            result.setCode(exception.getCode());
        }
        result.setMsg(errorMsg);
        result.setSuccess(Boolean.FALSE);
        return result;
    }

    public static Result<?> error(ResponseCodeEnum responseCodeEnum, String errorMsg) {
        Result<?> result = new Result<>();
        if (responseCodeEnum != null) {
            result.setCode(responseCodeEnum.getCode());
        }
        result.setMsg(errorMsg);
        result.setSuccess(Boolean.FALSE);
        return result;
    }

    public static Result<?> error(Exception exception, String errorMsg) {
        Result<?> result = new Result<>();
        if (exception != null) {
            result.setCode(ResponseCodeEnum.ERROR.getCode());
        }
        result.setMsg(errorMsg);
        result.setSuccess(Boolean.FALSE);
        return result;
    }

    public static Result<?> error(Integer errorCode, String errorMsg) {
        Result<?> result = new Result<>();
        result.setCode(errorCode);
        result.setMsg(errorMsg);
        result.setSuccess(Boolean.FALSE);
        return result;
    }

}
